package com.jumi.microservice.service.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.constant.compensation.AfterSaleStatusEnum;
import com.jumi.microservice.dto.admin.OrderGoodsEntityResponse;
import com.jumi.microservice.dto.admin.OrderIdGoodsIdRequest;
import com.jumi.microservice.dto.apiService.OrderGoodsResponse;
import com.jumi.microservice.dto.apiService.OrderInformationResponse;
import com.jumi.microservice.dto.serviceDto.*;
import com.jumi.microservice.entity.OrderGoods;
import com.jumi.microservice.entity.UserOrder;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.enumerate.SelfFlagEnum;
import com.jumi.microservice.mapper.LogisticsGoodsMapper;
import com.jumi.microservice.mapper.OrderGoodsMapper;
import com.jumi.microservice.mapper.UserOrderMapper;
import com.jumi.microservice.material.dto.supplier.JmSupplierResponse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.service.IJmSupplierService;
import com.jumi.microservice.material.service.IJmWarehouseService;
import com.jumi.microservice.service.IOrderDubboService;
import com.jumi.microservice.service.logistics.admin.SendService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangjingdao
 */
@Service
public class OrderServiceImpl {
    @Resource
    OrderGoodsMapper orderGoodsMapper;
    @Resource
    UserOrderMapper userOrderMapper;
    @Resource
    LogisticsGoodsMapper logisticsGoodsMapper;
    @DubboReference
    IJmSupplierService iJmSupplierService;
    @DubboReference
    IJmWarehouseService iJmWarehouseService;
    @DubboReference
    private IOrderDubboService orderDubboService;
    @Resource
    SendService sendService;


    public List<Integer> getOrderBySupplierId(int supplierId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.eq("supplier_id", supplierId);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }

    public List<Integer> getOrderByRealWarehouseId(int realWarehouseId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.eq("real_warehouse", realWarehouseId);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }

    public List<Integer> getOrderByBeginTime(String beginTime) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.ge("create_time", beginTime);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }

    public List<Integer> getOrderByEndTime(String endTime) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.le("create_time", endTime);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }

    public List<Integer> getOrderByGoodsName(String goodsName) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.like("goods_name", goodsName);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }

    public List<Integer> getOrderByPhone(String phone) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.eq("recevicer_phone", phone);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        return orderGoods.stream().map(OrderGoods::getOrderId).collect(Collectors.toList());
    }


    public List<Integer> getOrderByBuyerId(int buyerId) {
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        userOrderQueryWrapper.eq("buyer_id", buyerId);
        List<UserOrder> orders = userOrderMapper.selectList(userOrderQueryWrapper);
        if (orders == null) {
            return null;
        }
        return orders.stream().map(UserOrder::getId).collect(Collectors.toList());
    }

    public OrderGoodsEntityResponse getOrderGoodsDetails(OrderIdGoodsIdRequest orderIdGoodsIdRequest) {
        OrderGoodsEntityResponse orderDetailsResponse = new OrderGoodsEntityResponse();
        List<com.jumi.microservice.dto.admin.OrderGoodsResponse> orderGoodsResponses = new ArrayList<>();
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderIdGoodsIdRequest.getOrderId());
        queryWrapper.eq("after_seal_status", AfterSaleStatusEnum.NORMAL.getCode());
        queryWrapper.eq("goods_status", LogisticsStatusEnum.WAIT_SEND.getCode());
        //queryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(queryWrapper);
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        userOrderQueryWrapper.eq("id", orderIdGoodsIdRequest.getOrderId());
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        if (userOrder == null || orderGoods.size() == 0) {
            return null;
        }
        BeanUtils.copyProperties(userOrder, orderDetailsResponse);
        orderDetailsResponse.setPayType(userOrder.getPayType());
        for (OrderGoods orderGood : orderGoods) {
            com.jumi.microservice.dto.admin.OrderGoodsResponse orderGoodsResponse = new com.jumi.microservice.dto.admin.OrderGoodsResponse();
            BeanUtils.copyProperties(orderGood, orderGoodsResponse);
            if (orderGood.getSelfFlag().equals(SelfFlagEnum.YES_SELF.getCode())) {
                orderGoodsResponse.setSupplierId(orderGood.getRealWarehouse());
            } else {
                orderGoodsResponse.setSupplierId(orderGood.getSupplierId());
            }
            orderGoodsResponses.add(orderGoodsResponse);
        }
        orderDetailsResponse.setOrderGoodsResponses(orderGoodsResponses);
        return orderDetailsResponse;
    }

    public List<OrderInformationResponse> getOrderInformation(int[] orderIdArr) {
        List<Integer> orderId = Arrays.stream(orderIdArr).boxed().collect(Collectors.toList());
        List<OrderInformationResponse> orderInformationResponses = new ArrayList<>();
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.in("id", orderId);
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<UserOrder> userOrders = userOrderMapper.selectList(userOrderQueryWrapper);
        if (userOrders == null) {
            return null;
        }
        for (UserOrder userOrder : userOrders) {
            OrderInformationResponse orderInformationResponse = new OrderInformationResponse();
            orderInformationResponse.setOrderNo(userOrder.getOrderNo());
            orderInformationResponse.setId(userOrder.getId());
            orderInformationResponse.setPayTime(userOrder.getPayTime());
            orderInformationResponses.add(orderInformationResponse);
        }
        return orderInformationResponses;
    }

    public List<OrderGoodsResponse> getGoodsIdAndGoodsName(int[] goodsIdArr) {
        List<Integer> goodsId = Arrays.stream(goodsIdArr).boxed().collect(Collectors.toList());
        List<OrderGoodsResponse> orderGoodsResponses = new ArrayList<>();
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.in("goods_id", goodsId);
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        if (orderGoods == null || orderGoods.size() == 0) {
            return orderGoodsResponses;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = iJmWarehouseService.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        for (OrderGoods orderGood : orderGoods) {
            OrderGoodsResponse orderGoodsResponse = new OrderGoodsResponse();
            orderGoodsResponse.setGoodsId(orderGood.getGoodsId());
            orderGoodsResponse.setGoodsName(orderGood.getGoodsName());
            if (orderGood.getSelfFlag().equals(SelfFlagEnum.YES_SELF.getCode())) {
                jmWarehouseResponses.forEach(
                        jmWarehouseResponse -> {
                            if (orderGood.getRealWarehouse().equals(jmWarehouseResponse.getWarehouseId().intValue())) {
                                orderGoodsResponse.setSupplierName(jmWarehouseResponse.getWarehouseName());
                            }
                        }
                );
            } else if (orderGood.getSelfFlag().equals(SelfFlagEnum.NO_SELF.getCode())) {
                responses.forEach(
                        jmSupplierResponse -> {
                            if (orderGood.getSupplierId().equals(jmSupplierResponse.getSupplierId().intValue())) {
                                orderGoodsResponse.setSupplierName(jmSupplierResponse.getSupplierCompanyName());
                            }
                        }
                );
            }
            orderGoodsResponses.add(orderGoodsResponse);
        }
        return orderGoodsResponses;
    }

    public Boolean updateAfterSealStatus(UpdateAfterSealStatusRequest updateAfterSealStatusRequest) {
        boolean isStatus = false;
        updateAfterSealStatusRequest.checkRequest();
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", updateAfterSealStatusRequest.getOrderId());
        orderGoodsQueryWrapper.eq("goods_id", updateAfterSealStatusRequest.getGoodsId());
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods != null) {
            orderGoods.setAfterSealStatus(updateAfterSealStatusRequest.getAfterSealStatus());

            UpdateWrapper<OrderGoods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", updateAfterSealStatusRequest.getOrderId());
            updateWrapper.eq("goods_id", updateAfterSealStatusRequest.getGoodsId());
            //updateWrapper.eq("del_flag", DelFlag.NORMAL);
            int row = orderGoodsMapper.update(orderGoods, updateWrapper);
            if (row > 0) {
                isStatus = true;
            }
        }
        return isStatus;
    }

    public Boolean updateReturnInfo(UpdateReturnInfoRequest updateReturnInfoRequest) {
        boolean isStatus = false;
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", updateReturnInfoRequest.getOrderId());
        orderGoodsQueryWrapper.eq("goods_id", updateReturnInfoRequest.getGoodsId());
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods != null) {
            Integer returnCount = updateReturnInfoRequest.getReturnCount() != null ? updateReturnInfoRequest.getReturnCount() + orderGoods.getReturnCount() : orderGoods.getReturnCount();
            orderGoods.setReturnCount(returnCount);
            orderGoods.setReturnPrice(updateReturnInfoRequest.getReturnPrice().add(orderGoods.getReturnPrice()));
            orderGoods.setReturnPostage(updateReturnInfoRequest.getReturnPostage().add(orderGoods.getReturnPostage()));
            UpdateWrapper<OrderGoods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", updateReturnInfoRequest.getOrderId());
            updateWrapper.eq("goods_id", updateReturnInfoRequest.getGoodsId());
            //updateWrapper.eq("del_flag", DelFlag.NORMAL);
            int row = orderGoodsMapper.update(orderGoods, updateWrapper);
            if (row > 0) {
                isStatus = true;
            }
        }
        return isStatus;
    }

    public Boolean updateRefundInfo(UpdateRefundInfoRequest updateRefundInfoRequest) {
        boolean isStatus = false;
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", updateRefundInfoRequest.getOrderId());
        orderGoodsQueryWrapper.eq("goods_id", updateRefundInfoRequest.getGoodsId());
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods != null) {
            orderGoods.setAfterSealStatus(updateRefundInfoRequest.getAfterSealStatus());
            orderGoods.setReturnCount(updateRefundInfoRequest.getReturnCount() + orderGoods.getReturnCount());
            orderGoods.setReturnPrice(updateRefundInfoRequest.getReturnPrice().add(orderGoods.getReturnPrice()));
            orderGoods.setReturnPostage(updateRefundInfoRequest.getReturnPostage().add(orderGoods.getReturnPostage()));
            orderGoods.setRefundId(updateRefundInfoRequest.getRefundId());
            if (updateRefundInfoRequest.getGoodsStatus() >= LogisticsStatusEnum.CANCEL.getCode()) {
                orderGoods.setGoodsStatus(updateRefundInfoRequest.getGoodsStatus());
            }
            UpdateWrapper<OrderGoods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", updateRefundInfoRequest.getOrderId());
            updateWrapper.eq("goods_id", updateRefundInfoRequest.getGoodsId());
            //updateWrapper.eq("del_flag", DelFlag.NORMAL);
            orderGoodsMapper.update(orderGoods, updateWrapper);
        }
        isStatus = sendService.cancelLogistics(updateRefundInfoRequest.getOrderId());
        return isStatus;
    }

    public Boolean updateRefundGoodsInfo(UpdateRefundGoodsInfoRequest updateRefundGoodsInfoRequest) {
        boolean isStatus = false;
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", updateRefundGoodsInfoRequest.getOrderId());
        orderGoodsQueryWrapper.eq("goods_id", updateRefundGoodsInfoRequest.getGoodsId());
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods != null) {
            orderGoods.setAfterSealStatus(updateRefundGoodsInfoRequest.getAfterSealStatus());
            orderGoods.setRefundId(updateRefundGoodsInfoRequest.getRefundId());

            UpdateWrapper<OrderGoods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", updateRefundGoodsInfoRequest.getOrderId());
            updateWrapper.eq("goods_id", updateRefundGoodsInfoRequest.getGoodsId());
            //updateWrapper.eq("del_flag", DelFlag.NORMAL);
            int row = orderGoodsMapper.update(orderGoods, updateWrapper);
            if (row > 0) {
                isStatus = true;
            }
        }
        return isStatus;
    }

    public Integer getOrderStatus(int orderId) {
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.eq("id", orderId);
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        if (userOrder == null) {
            return null;
        }
        return userOrder.getOrderStatus().intValue();
    }

    public OrderStatusAfterSealResponse getStatusAfterSeal(int orderId, int goodsId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", orderId);
        orderGoodsQueryWrapper.eq("goods_id", goodsId);
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        OrderStatusAfterSealResponse orderStatusAfterSealResponse = new OrderStatusAfterSealResponse();
        BeanUtils.copyProperties(orderGoods, orderStatusAfterSealResponse);
        return orderStatusAfterSealResponse;
    }

    public Integer realSendAmount(int orderId, int goodsId) {
        Map<String, BigDecimal> map = logisticsGoodsMapper.getGoodsShouldRealSendAmount(orderId, goodsId);
        return map.get("realSendAmount").intValue();
    }

    public List<Integer> getOrderIdByCreateTime(Date createTime, Date endTime) {
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        userOrderQueryWrapper.between("create_time", createTime, endTime);
        List<UserOrder> userOrders = userOrderMapper.selectList(userOrderQueryWrapper);
        if (userOrders == null) {
            return null;
        }
        return userOrders.stream().map(UserOrder::getId).collect(Collectors.toList());
    }

    public Boolean updateOrderStatus(int orderId, byte orderStatus) {
        UpdateWrapper<UserOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", orderId);
        //updateWrapper.eq("del_flag", DelFlag.NORMAL);
        UserOrder userOrder = new UserOrder();
        userOrder.setOrderStatus(orderStatus);
        int row = userOrderMapper.update(userOrder, updateWrapper);
        return row != 0;
    }

    public OrderGoodsServiceDetailsResponse findOrderGoodsDetails(OrderGoodsServiceDetailsRequest request) {
        OrderGoodsServiceDetailsResponse orderGoodsServiceDetailsResponse = new OrderGoodsServiceDetailsResponse();
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", request.getOrderId());
        orderGoodsQueryWrapper.eq("goods_id", request.getGoodsId());
        orderGoodsQueryWrapper.in("goods_status", LogisticsStatusEnum.PART_SEND.getCode(), LogisticsStatusEnum.WAIT_RECEIVE.getCode(), LogisticsStatusEnum.CONFIRM_RECEIVE.getCode());
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        OrderGoods orderGoods = orderGoodsMapper.selectOne(orderGoodsQueryWrapper);
        if (orderGoods == null) {
            return null;
        }
        BeanUtils.copyProperties(orderGoods, orderGoodsServiceDetailsResponse);
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.eq("id", request.getOrderId());
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        if (userOrder == null) {
            return null;
        }
        BeanUtils.copyProperties(userOrder, orderGoodsServiceDetailsResponse);
        return orderGoodsServiceDetailsResponse;
    }

    public Date getConfirmTime(int orderId) {
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.eq("id", orderId);
        //userOrderQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        if (userOrder == null) {
            return null;
        }
        return userOrder.getConfirmTime();
    }

    public Boolean isExistAfterSale(int orderId) {
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        List<Integer> orderStaList = new ArrayList<>();
        orderStaList.add(AfterSaleStatusEnum.REFUND.getCode());
        orderStaList.add(AfterSaleStatusEnum.REFUND_GOODS_AGREE.getCode());
        orderGoodsQueryWrapper.eq("order_id", orderId);
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        orderGoodsQueryWrapper.and(queryWrapper -> {
            queryWrapper.notIn("after_seal_status", orderStaList).or().ne("goods_status", LogisticsStatusEnum.WAIT_RECEIVE.getCode());
        });
        List<OrderGoods> orderGoods = orderGoodsMapper.selectList(orderGoodsQueryWrapper);
        return orderGoods.size() > 0;
    }

    /**
     * 获取所有供应商
     *
     * @return
     */
    public List<JmSupplierResponse> getSupply() {
        ResponseResult<List<JmSupplierResponse>> responseResult = iJmSupplierService.list();
        List jmSupplierResponses = responseResult.getData();
        List<JmSupplierResponse> result = new ArrayList<>();
        for (Object object : jmSupplierResponses) {
            JmSupplierResponse jmSupplierResponse = JSON.parseObject(JSON.toJSONString(object), JmSupplierResponse.class);
            result.add(jmSupplierResponse);
        }
        return result;
    }

    /**
     * 订单商品表
     * 商品状态： -1 取消订单 0 待发货 1 部分发货 2 待收货 3 已完成
     * 订单商品表中是否存在 >-1 的商品状态
     *
     * @param orderId
     * @return
     */
    public Boolean orderGoodsStatus(int orderId) {
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", orderId);
        //queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.gt("goods_status", LogisticsStatusEnum.CANCEL.getCode());
        int row = orderGoodsMapper.selectCount(queryWrapper);
        return row > 0;
    }

    public Boolean updateInvoiceId(String orderNo, Integer invoiceId) {
        UpdateWrapper<UserOrder> userOrderUpdateWrapper = new UpdateWrapper<>();
        userOrderUpdateWrapper.eq("order_no", orderNo);
        UserOrder userOrder = new UserOrder();
        userOrder.setInvoiceId(invoiceId);
        return userOrderMapper.update(userOrder, userOrderUpdateWrapper) > 0;
    }

    /**
     * 商品出库中弹窗提示接口
     *
     * @param orderId
     * @param skuIdStr
     * @return
     */
    public Boolean isDelivery(Integer orderId, String skuIdStr) {
        boolean isStatus = false;
        //List<Integer> statusVals = new ArrayList<>();
        //statusVals.add(LogisticsStatusEnum.CANCEL.getCode().intValue());
        //statusVals.add(LogisticsStatusEnum.WAIT_SEND.getCode().intValue());
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id", orderId);
        orderGoodsQueryWrapper.in("goods_id", skuIdStr.split(","));
        orderGoodsQueryWrapper.eq("goods_status", LogisticsStatusEnum.WAIT_SEND.getCode().intValue());
        //orderGoodsQueryWrapper.in("goods_status", statusVals);
        //orderGoodsQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        Integer orderGoodsCount = orderGoodsMapper.selectCount(orderGoodsQueryWrapper);
        if (orderGoodsCount > 0) {
            isStatus = true;
        }
        return isStatus;
    }

    /**
     * 根据支付单号统计多个订单的总支付金额
     * 一个支付单号--对应多个订单
     *
     * @param payNo
     * @return
     */
    public BigDecimal getPayTotalAmount(String payNo) {
        QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(sum(pay_amount),0) AS pay_amount")
                .eq("pay_no", payNo);
        UserOrder userOrder = userOrderMapper.selectOne(queryWrapper);
        return userOrder.getPayAmount();
    }

    public Long getUidByOrderNo(String orderNo) {
        QueryWrapper<UserOrder> userOrderQueryWrapper = new QueryWrapper<>();
        userOrderQueryWrapper.lambda().eq(UserOrder::getOrderNo, orderNo);
        UserOrder userOrder = userOrderMapper.selectOne(userOrderQueryWrapper);
        return userOrder.getBuyerId();
    }
}
